package com.ssm.spring;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;

public class AopTest {
    public static void main(String[] args) {
        ApplicationContext applicationContext =
                new AnnotationConfigApplicationContext(AopConfig.class);
        UserService userService = applicationContext.getBean(UserService.class);
        userService.login("admin", "123456");
    }
}

@EnableAspectJAutoProxy
@Configuration
@Import({UserService.class, ValidateAspect.class})
class AopConfig {


}

@Component
class UserService {
    public String login(String username, String password) {
        System.out.println("username：" + username + ",password:" + password);
        return "ok";
    }
}

@Aspect
@Component
class ValidateAspect {

    @Pointcut("execution(public * com.ssm.spring.UserService.*(..))")
    public void servicePoint() {
        // Do nothing
    }


    @Around("servicePoint()")
    public Object doAroundService(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("validate param invoke !!!");
        return joinPoint.proceed();
    }
}

